package cn.edu.sdyu.ie.crs.iot;

import cn.edu.sdyu.ie.crs.domain.ClassroomEnvironment;
import cn.edu.sdyu.ie.crs.domain.Device;
import cn.edu.sdyu.ie.crs.domain.DeviceRunLog;
import cn.edu.sdyu.ie.crs.service.ClassroomEnvironmentService;
import cn.edu.sdyu.ie.crs.service.DeviceRunLogService;
import cn.edu.sdyu.ie.crs.service.DeviceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("mqtt")
public class MqttWebHook {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ClassroomEnvironmentService classroomEnvironmentService;
    @Autowired
    private DeviceRunLogService deviceRunLogService;

    @PostMapping("environment")
    public void handlerEnvironmentData(@RequestBody MqttMessage mqttMessage) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        EnvironmentData environmentData = mapper.readValue(mqttMessage.getPayload(), EnvironmentData.class);

        Device device = deviceService.findDeviceByDeviceCode(environmentData.getDeviceCode());
        if(environmentData.getSensorStatus() == 1) {

            ClassroomEnvironment classroomEnvironment = classroomEnvironmentService.findClassroomEnvironmentByClassroomId(device.getClassroomId());
            classroomEnvironment.setId(device.getClassroomId());
            classroomEnvironment.setTemperature(environmentData.getTemperature());
            classroomEnvironment.setHumidity(environmentData.getHumidity());
            classroomEnvironment.setUpdateTime(new Date());
            classroomEnvironmentService.saveClassroomEnvironment(classroomEnvironment);

            DeviceRunLog deviceRunLog = new DeviceRunLog();
            deviceRunLog.setDeviceName(device.getDeviceName());
            deviceRunLog.setDeviceCode(device.getDeviceCode());
            deviceRunLog.setDeviceType(device.getDeviceType());
            deviceRunLog.setLogType(2);
            deviceRunLog.setOperationType(3);
            deviceRunLog.setEvent(device.getDeviceCode() + ": 采集信息");
            deviceRunLog.setTime(new Date());
            deviceRunLog.setCreateTime(new Date());
            deviceRunLogService.saveDeviceRunLog(deviceRunLog);

        } else {
            DeviceRunLog deviceRunLog = new DeviceRunLog();
            deviceRunLog.setDeviceName(device.getDeviceName());
            deviceRunLog.setDeviceCode(device.getDeviceCode());
            deviceRunLog.setDeviceType(device.getDeviceType());
            deviceRunLog.setLogType(2);
            deviceRunLog.setOperationType(3);
            deviceRunLog.setEvent(device.getDeviceCode() + ": 采集信息异常");
            deviceRunLog.setTime(new Date());
            deviceRunLog.setCreateTime(new Date());
            deviceRunLogService.saveDeviceRunLog(deviceRunLog);
            log.warn("传感器数据异常");

        }
    }
}
